package com.dy.course.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dy.course.common.BaseException;
import com.dy.course.common.BaseService;
import com.dy.course.common.Constant;
import com.dy.course.mapper.CourseNumTimeMapper;
import com.dy.course.model.CourseNumTime;
import com.dy.course.service.ICourseNumTimeService;
import com.dy.course.util.DateUtil;

import tk.mybatis.mapper.entity.Example;

@Service
public class CourseNumTimeService extends BaseService<CourseNumTime> implements ICourseNumTimeService{

	
	@Autowired
	private CourseNumTimeMapper courseNumTimeMapper;
	
	
	@Override
	public List<CourseNumTime> getAll() {
		return getMapper().selectAll();
	}

	@Override
	public CourseNumTime getById(Long id) {
		return null;
	}

	@Override
	public CourseNumTime getByKey(Date date,Long universityId, Integer number) {
		logger.info("【根据大学,第几节课查询时间信息】参数:universityId={},number={}",universityId,number);
		String key = Constant.CacheKey.COURSE_NUM_TIME + "#" +universityId+"#"+number+DateUtil.getSeason(date);
		CourseNumTime cnt = (CourseNumTime) redisCache.get(key);
		if(cnt != null)
			return cnt;
		Example example = new Example(CourseNumTime.class);
		example.createCriteria().andEqualTo("universityId", universityId).andEqualTo("number", number);
		List<CourseNumTime> result = getMapper().selectByExample(example);
		if(result == null||result.isEmpty()){
			logger.error("数据被删除或者不存在,参数:universityId={},number={}",universityId,number);
			throw new BaseException(Constant.Code.SELECT_ERROR,"数据被删除或者不存",null);
		}
		return result.get(0);
	}

	@Override
	public List<Map<String, Object>> getCourseNums() {
		logger.info("【查询每个大学有多少课】参数:{}");
		return this.courseNumTimeMapper.getCourseNums();
	}

}
